package com.qm.finger.protocol;


import com.qm.finger.SerialManagerClass;
import com.qm.utils.ByteUtil;
import com.qm.utils.FinerResultEnum;

import java.io.InputStream;

public class FingerSdk {
    private static final String TAG = "LockerSdk";
    SerialManagerClass serialManger = null;


    public Boolean getOK() {
        return isOK;
    }

    Boolean isOK = false;

    public InputStream Init(String port) {
        if (serialManger == null)
            serialManger = new SerialManagerClass();
        else if (serialManger.isSerialPortOpened())
            serialManger.closeSerialPort();
        return serialManger.openSerialPort(port, 9600);
    }

    public InputStream getInputStream() {
        if (serialManger != null) {
            return serialManger.getInputStream();
        } else {
            return null;
        }
    }

    //第一次采集指纹
    public int startFinger_1(int userID) {
        byte[] sendDat = FingerProtocol.BuildAddFinger_1(userID);
        return serialManger.sendFingerCommand(sendDat);
    }

    //第二次采集指纹
    public int startFinger_2(int userID) {
        byte[] sendDat = FingerProtocol.BuildAddFinger_2(userID);
        return serialManger.sendFingerCommand(sendDat);
    }

    //第三次采集指纹
    public int startFinger_3(int userID) {
        byte[] sendDat = FingerProtocol.BuildAddFinger_3(userID);
        return serialManger.sendFingerCommand(sendDat);
    }

    //获取用户数量
    public int getFingerUserNum() {
        byte[] sendDat = FingerProtocol.BuildGetUserNum();
        byte[] retDat = new byte[20];
        int retLen = serialManger.sendCommand(true, sendDat, retDat);
        if (retLen <= 0 || retLen > retDat.length)
            return FingerProtocol.Resp_NoAnswer;
        int ret = FingerProtocol.ParseBBCProtocol(retDat);
        if (ret == FinerResultEnum.ACK_FAIL.ordinal()) {
            return FingerProtocol.Resp_NoAnswer;
        } else {
            byte[] bt = new byte[2];
            System.arraycopy(retDat, 2, bt, 0, bt.length);
            return ByteUtil.byteArrayToInt(bt);
        }
    }

    //删除所有的用户
    public int deleteAllUser(int userRole) {
        byte[] sendDat = FingerProtocol.BuildDeleteAllUser(userRole);
        byte[] retDat = new byte[20];
        int retLen = serialManger.sendCommand(true, sendDat, retDat);
        if (retLen <= 0 || retLen > retDat.length)
            return FingerProtocol.Resp_NoAnswer;
        int ret = FingerProtocol.ParseBBCProtocol(retDat);
        if (ret == FinerResultEnum.ACK_FAIL.ordinal()) {
            return FingerProtocol.Resp_NoAnswer;
        }
        {
            return retDat[4];
        }
    }
    //删除指定用户
    public int deleteUser(int userId) {
        byte[] sendDat = FingerProtocol.BuildDeleteUserByID(userId);
        byte[] retDat = new byte[20];
        int retLen = serialManger.sendCommand(true, sendDat, retDat);
        if (retLen <= 0 || retLen > retDat.length)
            return FingerProtocol.Resp_NoAnswer;
        int ret = FingerProtocol.ParseBBCProtocol(retDat);
        if (ret == FinerResultEnum.ACK_FAIL.ordinal()) {
            return FingerProtocol.Resp_NoAnswer;
        }
        {
            return retDat[4];
        }
    }
    //1:N比对用户特征值
    public int compareUser() {
        byte[] sendDat = FingerProtocol.BuildCompare1_N();
        return serialManger.sendFingerCommand( sendDat);
    }
    //采集图像并进行提取特征值
    public int collectImgExtractF(){
        byte[] sendDat = FingerProtocol.BuildCollectIMGExtractFeature();
        return serialManger.sendFingerCommand(sendDat);
    }
    //将提取的指纹信息写入指纹模块
    public int registerFinger(int userID,byte[] fingerData){
        byte[] sendDat = FingerProtocol.BuildRegisterUser(userID,fingerData);
        byte[] retDat = new byte[20];
        int retLen = serialManger.sendCommand(true, sendDat, retDat);
        if (retLen <= 0 || retLen > retDat.length)
            return FingerProtocol.Resp_NoAnswer;
        int ret = FingerProtocol.ParseBBCProtocol(retDat);
        if (ret == FinerResultEnum.ACK_FAIL.ordinal()) {
            return FingerProtocol.Resp_NoAnswer;
        }
        {
            return retDat[4];
        }
    }
    public void UnInit() {
        if (serialManger != null)
            serialManger.closeSerialPort();
    }

}
